package showhow.simple.service;

import showhow.simple.common.enums.CustomerLevel;
import showhow.simple.model.entity.CustomerDO;
import showhow.simple.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
public class CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    public CustomerDO getByNameAndPaswd(String name, String paswd) {
        return customerMapper.getByNameAndPaswd(name, paswd);
    }

    /** 计算实际费用。不同等级有不同的折扣。 */
    public Double calculateActualFee(Long customerId, Double totalFee) {
        CustomerDO customerDO = customerMapper.selectById(customerId);
        CustomerLevel level = CustomerLevel.get(customerDO.getLevel());
        BigDecimal actualFee = new BigDecimal(totalFee);
        switch (level) {
            case LEVEL_1:
                actualFee = BigDecimal.valueOf(totalFee).multiply(
                        BigDecimal.valueOf(CustomerLevel.LEVEL_1.getDiscount())
                );
                break;
            case LEVEL_2:
                actualFee = BigDecimal.valueOf(totalFee).multiply(
                        BigDecimal.valueOf(CustomerLevel.LEVEL_2.getDiscount())
                );
                break;
            case LEVEL_3:
                actualFee = BigDecimal.valueOf(totalFee).multiply(
                        BigDecimal.valueOf(CustomerLevel.LEVEL_3.getDiscount())
                );
                break;
        }
        return actualFee.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
}
